/*

    This file is part of GLua

    GLua Development Environment
    Copyright (C) 2007 VoiDeD

    GLua is free software; you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    GLua is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

*/
using System;
using System.Collections.Generic;
using System.Text;

namespace GLua
{
    /// <summary>
    /// Represents a class which contains versioning information and is comparable to other classes
    /// </summary>
    // more of a TODO class, was going to be used for updating
    public class ComparableVersion
    {
        int ver;
        int extType;
        int extVer;

        public ComparableVersion( GLuaVersionAttribute attrib )
        {
            try
            {
                ver = Convert.ToInt32( attrib.Version );
                extType = ( int )attrib.ExtendedType;
                extVer = Convert.ToInt32( attrib.ExtendedVersion );
            }
            catch
            {
                Util.ShowError( StringTable.UnableToCompareVerions );
                return;
            }
        }
        public ComparableVersion( VersionInformation ver )
        {
            try
            {
                string strVer = ver.LastestVersion.Split( ',' )[0];
                VersionType type = ( VersionType )Enum.Parse( typeof( VersionType ), ver.LastestVersion.Split( ',' )[1] );
                string strExtVer = ver.LastestVersion.Split( ',' )[2];

                this.ver = Convert.ToInt32( strVer );
                extType = ( int )type;
                extVer = Convert.ToInt32( strExtVer );
            }
            catch
            {
                Util.ShowError( StringTable.UnableToCompareVerions );
                return;
            }

        }
        public ComparableVersion( string ver )
        {
            try
            {
                string strVer = ver.Split( ',' )[0];
                VersionType type = ( VersionType )Enum.Parse( typeof( VersionType ), ver.Split( ',' )[1] );
                string strExtVer = ver.Split( ',' )[2];

                this.ver = Convert.ToInt32( strVer );
                extType = ( int )type;
                extVer = Convert.ToInt32( strExtVer );
            }
            catch
            {
                Util.ShowError( StringTable.UnableToCompareVerions );
                return;
            }
        }

        /// <summary>
        /// Compares the current <c>ComparableVersion</c> with another
        /// </summary>
        /// <param name="ver">The version to compare against</param>
        /// <returns>-1 if the current version is newer than <paramref name="ver"/>, 0 if the versions are equal, 1 if the current version is older than <paramref name="ver"/></returns>
        public int CompareWith( ComparableVersion ver )
        {
            if ( this.ver > ver.ver )
            {
                return -1;
            }
            else if ( this.ver < ver.ver )
            {
                return 1;
            }
            else
            {
                if ( this.extType > ver.extType )
                {
                    return -1;
                }
                else if ( this.extType < ver.extType )
                {
                    return 1;
                }
                else
                {
                    if ( this.extVer > ver.extVer )
                    {
                        return -1;
                    }
                    else if ( this.extVer < ver.extVer )
                    {
                        return 1;
                    }
                    else
                    {
                        return 0;
                    }
                }
            }
        }

        public static bool operator <(ComparableVersion val1, ComparableVersion val2)
        {
            if ( val1.ver < val2.ver )
            {
                return true;
            }
            else
            {
                if ( val1.extType < val2.extType )
                {
                    return true;
                }
                else
                {
                    if ( val1.extVer < val2.extVer )
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }
        public static bool operator >( ComparableVersion val1, ComparableVersion val2 )
        {
            if ( val1.ver > val2.ver )
            {
                return true;
            }
            else
            {
                if ( val1.extType > val2.extType )
                {
                    return true;
                }
                else
                {
                    if ( val1.extVer > val2.extVer )
                    {
                        return true;
                    }
                    else
                    {
                        return false;
                    }
                }
            }
        }

        public static bool operator ==( ComparableVersion val1, ComparableVersion val2 )
        {
            if ( val1.ver == val2.ver )
                if ( val1.extType == val2.extType )
                    if ( val1.extVer == val2.extVer )
                        return true;

            return false;
        }
        public static bool operator !=( ComparableVersion val1, ComparableVersion val2 )
        {
            if ( val1.ver != val2.ver )
                if ( val1.extType != val2.extType )
                    if ( val1.extVer != val2.extVer )
                        return true;

            return false;
        }

        public override bool Equals( object obj )
        {
            return base.Equals( obj );
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
    }
}